/**
 * 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
 */

//let isIos = false;
// 判断麦克风权限是否开启
function judgeIosPermissionRecord() {
	let result = false;
	let avaudiosession = window.plus.ios.import("AVAudioSession");
	let avaudio = avaudiosession.sharedInstance();
	let permissionStatus = avaudio.recordPermission();
	console.log("permissionStatus:" + permissionStatus);
	if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
		console.log("麦克风权限没有开启");
	} else {
		result = true;
		console.log("麦克风权限已经开启");
	}
	window.plus.ios.deleteObject(avaudiosession);
	return result;
}

// 判断相机权限是否开启
function judgeIosPermissionCamera() {
	let result = false;
	let AVCaptureDevice = window.plus.ios.import("AVCaptureDevice");
	let authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
	console.log("authStatus:" + authStatus);
	if (authStatus == 3) {
		result = true;
		console.log("相机权限已经开启");
	} else {
		console.log("相机权限没有开启");
	}
	window.plus.ios.deleteObject(AVCaptureDevice);
	return result;
}


// Android权限查询
function requestAndroidPermission(permissionID) {
	return new Promise((resolve, reject) => {
		window.plus.android.requestPermissions(
			[permissionID], // 理论上支持多个权限同时查询，但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
			function(resultObj) {
				let result = 0;
				for (let i = 0; i < resultObj.granted.length; i++) {
					let grantedPermission = resultObj.granted[i];
					console.log('已获取的权限：' + grantedPermission);
					result = 1
				}
				for (let i = 0; i < resultObj.deniedPresent.length; i++) {
					let deniedPresentPermission = resultObj.deniedPresent[i];
					console.log('拒绝本次申请的权限：' + deniedPresentPermission);
					result = 0
				}
				for (let i = 0; i < resultObj.deniedAlways.length; i++) {
					let deniedAlwaysPermission = resultObj.deniedAlways[i];
					console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
					result = -1
				}
				resolve(result);
			},
			function(error) {
				console.log('申请权限错误：' + error.code + " = " + error.message);
				resolve({
					code: error.code,
					message: error.message
				});
			}
		);
	});
}



export default {
	judgeIosPermissionRecord: judgeIosPermissionRecord,
	judgeIosPermissionCamera: judgeIosPermissionCamera,
	requestAndroidPermission: requestAndroidPermission
}
